<?php
class Navigation extends MySQL_Connector {
	// Variablen
	private $format = "";

	public function __construct($format = "<div>%text%</div>") {
		// connect to the mysql server
		parent::__construct();
		
		$this->format = $format;
		
		$this -> _get_year_id = $this -> get_numeric_id("cash_book_id");
		$this -> _get_month_id = $this -> get_numeric_id("cash_book_month_id");
		$this -> _get_category_id = $this -> get_numeric_id("category");
	}
	
	/*
	 * Closing the connection to the mysql server
	 */
	public function __destruct() {
		parent::__destruct();
	}
	
	/*
	 * Gibt einen Menupunkt aus.
	 */
	public function show_menu_item(navigation_item $item) {
		$rangs = general::get_rangs();
		if($rangs[$_SESSION["rang"]] >= $rangs[$item->rang]) {
			$link = str_replace("%page%", $item->_get_page, $this->format);
			$link = str_replace("%title%", $item->title, $link);
			echo $link;
		}
	}
	
	public function show_links() {
		$sql = "SELECT * FROM site ORDER BY `index` ASC";
		$result = $this -> con -> query($sql);
		while ($row = mysqli_fetch_array($result)) {
			$this->show_menu_item(new navigation_item(0, $row["title"], $row["page"], $row["rang"]));
		}
	}
	
	/*
	 * Prints a specific navigation-header.
	 */
	public function show_navigation_header($title) {
		echo "<div id=\"navi_header\">".$title."</div>";
	}
	
	// Database-IDs
	public $year_id;
	public $month_id;
	public $category_id;
	// Display-Names
	public $year_title;
	public $month_title;
	public $category_title;
	// GET-Variablen
	public $_get_year_id;
	public $_get_month_id;
	public $_get_category_id;

	/*
	 * Give all cashbooks out depending of the database (actually based on an array).
	 */
	public function show_cash_book_links() {
		$query = "	SELECT * FROM year";
		$result = $this -> con -> query($query, MYSQLI_STORE_RESULT);

		while ($row = $result -> fetch_array(MYSQLI_ASSOC)) {
			$this -> year = $row["year"];
			$this -> year_id = $row["id"];
			echo "<a href=\"index.php?page=cash_book&cash_book_id=" . $this -> year_id . "\">
					<div class=\"links_cashbook_header\">
						" . $this -> year . "
					</div>
				  </a>";
			if ($this -> _get_year_id == $this -> year_id && $this -> _get_year_id != "NULL") {
				$this -> display_months();
			}
		}
	}
	
	/*
	 * Gets all months of an cashbook.
	 */
	private function display_months() {
		$query = "	SELECT * FROM month";
		$result = $this -> con -> query($query, MYSQLI_STORE_RESULT);

		while ($row = $result -> fetch_array(MYSQLI_ASSOC)) {
			$this -> month_id = $row["id"];
			$this -> month_title = $row["title"];
			echo "<a href=\"index.php?page=cash_book&cash_book_id=" . $this -> year_id . "&cash_book_month_id=" . $this -> month_id . "\">
					<div class=\"links tab\">
						 " . $this -> month_title . "
					</div>
				  </a>";
			if ($this -> _get_month_id == $this -> month_id && $this -> _get_month_id != "NULL") {
				$this -> display_categories();
			}
		}
	}
	
	/*
	 * Gets all categories of a months.
	 */
	private function display_categories() {
		$query = "	SELECT c.id, c.title FROM year y
					INNER JOIN year_category y_c
						ON y_c.year_id = y.id
					INNER JOIN category c
						ON c.id = y_c.category_id AND y_c.month_id = " . $this -> month_id . "
					WHERE y.id = " . $this -> year_id;

		if ($stmt = $this -> con -> prepare($query)) {
			$stmt -> execute();
			$stmt -> bind_result($this -> category_id, $this -> category_title);

			while ($stmt -> fetch()) {
				echo "<a href=\"index.php?page=cash_book&cash_book_id=" . $this -> year_id . "&cash_book_month_id=" . $this -> month_id . "&category=" . $this -> category_id . "\">
						<div class=\"links_cashbook_header\">
							" . $this -> category_title . "
						</div>
					  </a>";
			}
		}
	}

	public function get_numeric_id($get_variable_name) {
		if (isset($_GET[$get_variable_name])) {
			if (is_numeric($_GET[$get_variable_name])) {
				return htmlspecialchars($_GET[$get_variable_name]);
			} else {
				return "NULL";
			}
		} else {
			return "NULL";
		}
	}
}
?>